//推荐技师

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:hxdj/common/base_page/my_loading_page.dart';
import 'package:hxdj/common/components/my_clip_rect.dart';
import 'package:hxdj/common/components/my_smart_refresher.dart';
import 'package:hxdj/common/components/my_statistic.dart';
import 'package:hxdj/common/components/my_tap_widget.dart';
import 'package:hxdj/common/components/my_text.dart';
import 'package:hxdj/common/my_header_global.dart';
import 'package:hxdj/common/routers/navigator.dart';
import 'package:hxdj/home/project_details/project_details_page.dart';
import 'package:hxdj/recommend_project/recommend_project_viewmodel.dart';
import 'package:provider/provider.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

//推荐项目
class RecommendProjectPage extends StatefulWidget {
  static String routeName = "/RecommendProjectPage";

  const RecommendProjectPage({super.key});

  @override
  State<RecommendProjectPage> createState() => _RecommendProjectPageState();
}

class _RecommendProjectPageState extends State<RecommendProjectPage> {
  //viewModel
  late RecommendProjectViewModel viewModel;

  late ScrollController crollController = ScrollController();

  late RefreshController refreshController = RefreshController();


  @override
  void initState() {
    super.initState();
  }

  @override
  dispose() {
    super.dispose();
  }

  _buildBody() {
    switch (viewModel.state) {
      case ViewState.loading:
        return const MyLoadingWidget();
      default:
        return Column(children: [
          // MyBannerView([
          //   'https://picsum.photos/800/600?t=111',
          //   'https://picsum.photos/800/600?t=111222',
          //   'https://picsum.photos/800/600?t=111333',
          //   'https://picsum.photos/800/600?t=11144',
          //   'https://picsum.photos/800/600?t=111444',
          //   'https://picsum.photos/800/600?t=1113343',
          // ], select: (int index) {}),
          Expanded(
              child: MySmartRefresher(
            enablePullUp: false,
            enablePullDown: true,
            onRefresh: () async {
              await Future.delayed(const Duration(milliseconds: 500));
              viewModel.requestData();
            },
            customChildWidget: ListView.builder(
                padding: EdgeInsets.only(top: 15.h),
                itemBuilder: (cte, index) {
                  ProjectModel projectModel = viewModel.dataList[index];
                  return ProjectItem(context: context, projectModel: projectModel);
                },
                itemCount: viewModel.dataList.length),
            controller: refreshController,
            scrollController: crollController,
          ))
        ]);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<RecommendProjectViewModel>(builder: (context, model, child) {
      viewModel = model;

      //刷新完成
      refreshController.refreshToIdle();
      return Scaffold(
          backgroundColor: MyColours.materialBg,
          appBar: const MyAppBar(isBack: false, title: "推荐项目"),
          body: _buildBody());
    });
  }
}

class ProjectItem extends StatelessWidget {
  const ProjectItem({
    super.key,
    required this.context,
    required this.projectModel,
  });

  final BuildContext context;
  final ProjectModel projectModel;

  @override
  Widget build(BuildContext context) {
    return MyTapWidget(
      onTap: () {
        // 项目详情
        NavigatorUtils.pushParams(context, '${PackageDetailPage.routeName}?gId=${projectModel.id}');
      },
      child: Container(
        margin: EdgeInsets.fromLTRB(16.w, 0, 16.w, 15.h),
        child: MyBorder(
          radius: 11.w,
          decoration: MyBorder.defaultDecoration,
          child: Row(
            children: [
              MyClipRect(imageUrl: projectModel.logo ?? "", width: 110.w, radius: 11.w, height: 110.w),
              Expanded(
                child: Container(
                  padding: EdgeInsets.only(left: 10.w, top: 5.h),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      MyText(projectModel.name ?? "",
                          fontSize: 14.sp, fontWeight: FontWeight.bold, color: MyColours.text),
                      Container(
                          padding: EdgeInsets.only(top: 5.h, bottom: 5.h),
                          child: MyText("${projectModel.serviceDuration ?? ""} | ${projectModel.titleDesc ?? ""}",
                              fontSize: 12.sp, color: MyColours.textGray)),

                      // 套餐价格
                      Container(
                        child: MyStatisticViewModel(
                          statistic: num.parse(projectModel.price ?? "0"),
                          statisticTS:
                              TextStyle(fontSize: 18.sp, color: MyColours.colorFF4414, fontWeight: FontWeight.w700),
                          withName: '¥' " ",
                          withNameTS:
                              TextStyle(fontSize: 15.sp, color: MyColours.colorFF4414, fontWeight: FontWeight.w700),
                          statisticDecimalTS: TextStyle(color: MyColours.colorFF4414, fontSize: 15.sp),
                          prefix: '',
                          suffix: '',
                          isCurrency: false,
                          toStringAsFixed: 0,
                        ).toRichText,
                      ),

                      Row(
                        children: [
                          MyText(
                            "¥${projectModel.goodsDiscountPrice}",
                            fontSize: 14.sp,
                            color: MyColours.color73736EB,
                            fontWeight: FontWeight.bold,
                          ),
                          Container(
                              margin: EdgeInsets.only(left: 6.w),
                              color: MyColours.colorE7C8A6,
                              padding: EdgeInsets.symmetric(horizontal: 2.w, vertical: 2.w),
                              alignment: Alignment.center,
                              child: Text("会员价",
                                  style: TextStyle(
                                    color: MyColours.color8E6447,
                                    fontSize: 8.sp,
                                    fontWeight: FontWeight.w700,
                                  ))),
                        ],
                      ),
                    ],
                  ),
                ),
              ),
              Container(
                margin: EdgeInsets.only(right: 10.w),
                child: Column(
                  children: [
                    Row(mainAxisAlignment: MainAxisAlignment.center, children: [
                      Container(
                        decoration: BoxDecoration(
                          color: MyColours.appMain,
                          borderRadius: BorderRadius.circular(15),
                        ),
                        padding: EdgeInsets.symmetric(horizontal: 10.5.w, vertical: 4.5.h),
                        alignment: Alignment.center,
                        child: Text("去预约",
                            style: TextStyle(
                              color: Colors.white,
                              fontSize: 12.sp,
                              fontWeight: FontWeight.w700,
                            )),
                      ),
                    ]),
                    Padding(
                      padding: EdgeInsets.only(top: 5.h),
                      child:
                          MyText("30天已售${projectModel.sales}单",
                          fontSize: 9.sp, fontWeight: FontWeight.bold, color: MyColours.textGrayA),
                    ),
                  ],
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}
